<?php
    namespace Core\ORM;

    /**
     * Un Where est une condition pour une Requête SQL
     *
     * @author mpetrini
     */
    class Where {
        /**
        * @var \Core\ORM\Table $table
        */
        protected $table;
        /**
        * @var \Core\ORM\Attributs\Attribut $attribut
        */
        protected $attribut;
        /**
        * @var mixed $valeur
        */
        protected $valeur;
        
        /**
        * Constructeur complet
        * 
        * @throw \Core\ORM\Exception\WhereException
        * 
        * @param \Core\ORM\Table $table
        * @param string $nomAttribut
        * @param mixed $valeur
        * 
        * @return void
        */
        public function __construct($table, $nomAttribut, $valeur) {
            $this->attribut = $table->getAttributParNom($nomAttribut);
            if($this->attribut instanceof \Core\ORM\Attributs\Objet) {
                if($valeur === null && $this->attribut->getNull()) {
                    $this->attribut = new \Core\ORM\Attributs\Integer("profil_id");
                    $this->table = $table;
                }
                else if($valeur === null) {
                    $this->table=$table;
                    throw new \Core\ORM\Exception\WhereException($this);
                }
                else {
                    $this->table = new \Core\ORM\Table($this->attribut->getNomClasse());
                    $this->table->setParent($table);
                }
            }
            else {
                $this->table = $table;
            }
            $this->valeur = $valeur;
        }
        
        /**
        * Crée la reqûete Sql pour un attribut Where
        * 
        * @param string $precedent (optional) (default="")
        * 
        * @return string
        */
        public function creationRequeteWhere($precedent = "") {
            $alias = $precedent.$this->table->getAliasComplet();
            $compteur = 0;
            $sql = '';
            foreach($this->table->getChamps() as $attribut) {
                if($attribut instanceof \Core\ORM\Attributs\Objet) {
                    $getter = 'get'.\Utils\Outils::toCamelCase($attribut->getNomClasse(), true);
                    $where = new \Core\ORM\Where(new Table(get_class($this->getValeur()->$getter())), $attribut->getNom(), $this->getValeur()->$getter());
                    $sql .= " AND ".$where->creationRequeteWhere($alias."_");
                }
                else {
                    $getter = 'get'.\Utils\Outils::toCamelCase($attribut->getNom(), true);
                    if($compteur !== 0) {
                        $sql .= ' AND ';
                    }
                    $sql .= $alias.'.`'.$attribut->getNom().'`='.\Core\ORM\ORMClasse::$lien->quote($this->getValeur()->$getter());
                    $compteur ++;
                }
            }
            return $sql;
        }
        
        //GETTERS ET SETTERS
        public function getTable() {return $this->table;}
        public function setTable($table) {$this->table=$table;}
        public function getAttribut() {return $this->attribut;}
        public function setAttribut($attribut) {$this->attribut=$attribut;}
        public function getValeur() {return $this->valeur;}
        public function setValeur($valeur) {$this->valeur=$valeur;}
    }
?>